package com.sj.configuration.json;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;

public class JFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter{
    public static final Charset UTF8 = Charset.forName("UTF-8");

	@Override
	protected void writeInternal(Object object, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		if(object instanceof Model) {
			OutputStream out = outputMessage.getBody();
			out.write(JsonKit.toJson(object).getBytes(UTF8));
		}else {
			super.writeInternal(object, outputMessage);
		}
	}
	
}
